package com.ymatou.iapi.liveseller.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.SaveLiveNoticeBean;
import com.ymatou.iapi.liveseller.service.SaveLiveNoticeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.TestDataManager;
/**
*保存公告
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SaveLiveNotice{
	private static SaveLiveNoticeBean savelivenoticeBean;
	private static SaveLiveNoticeCall savelivenoticeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("保存公告");
	}

	@Before
	public void caseUp() {
		savelivenoticeBean=new SaveLiveNoticeBean();
		savelivenoticeCall=new SaveLiveNoticeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(savelivenoticeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer liveNoticeId, SaveLiveNoticeBean savelivenoticeBean2) throws InterruptedException {
		Map m=new LiveRoomWapper().selectLivenotice(liveNoticeId).get(0);
		Logger.debug(m);
		Logger.verifyEquals(savelivenoticeBean2.getLiveId(), m.get("LiveId"), "验证LiveId");
		Logger.verifyEquals(savelivenoticeBean2.getContent(), m.get("Content"), "验证Content");
		Logger.verifyEquals(savelivenoticeBean2.getOperator(), m.get("LastOperator"), "验证LastOperator");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveLiveNotice_001() {
		Logger.start(true,"创建公告,成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			savelivenoticeBean.setContent("Content");
			savelivenoticeBean.setLiveId(livedto.getId());
			savelivenoticeBean.setOperator("autotest");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("200", savelivenoticeCall.getString("Code"), "验证code");
			Logger.verifyNotNull(savelivenoticeCall.getLiveNoticeId(), "验证LiveNoticeId");
			verifyDB(savelivenoticeCall.getLiveNoticeId(),savelivenoticeBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveLiveNotice_002() {
		Logger.start(true,"修改公告,成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			savelivenoticeBean.setContent("Content");
			savelivenoticeBean.setLiveId(livedto.getId());
			savelivenoticeBean.setOperator("autotest");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("200", savelivenoticeCall.getString("Code"), "验证code");
			Logger.verifyNotNull(savelivenoticeCall.getLiveNoticeId(), "验证LiveNoticeId");
			verifyDB(savelivenoticeCall.getLiveNoticeId(),savelivenoticeBean);
			savelivenoticeBean.setLiveNoticeId(savelivenoticeCall.getLiveNoticeId());
			savelivenoticeBean.setContent("Content2");
			savelivenoticeBean.setLiveId(livedto.getId());
			savelivenoticeBean.setOperator("autotest2");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("200", savelivenoticeCall.getString("Code"), "验证code");
			Logger.verifyNotNull(savelivenoticeCall.getLiveNoticeId(), "验证LiveNoticeId");
			verifyDB(savelivenoticeCall.getLiveNoticeId(),savelivenoticeBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveLiveNotice_003() {
		Logger.start(true,"内容超过30字失败");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			savelivenoticeBean.setContent("Content"+TestDataManager.getRandomNum(60));
			savelivenoticeBean.setLiveId(livedto.getId());
			savelivenoticeBean.setOperator("autotest");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("201", savelivenoticeCall.getString("Code"), "验证code");
//			Logger.verifyNotNull(savelivenoticeCall.getLiveNoticeId(), "验证LiveNoticeId");
			//直播公告长度为最多为30个汉字或60个英文字符
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveLiveNotice_004() {
		Logger.start(true,"直播不存在失败");
		try {
			savelivenoticeBean.setContent("Content");
			savelivenoticeBean.setLiveId(1);
			savelivenoticeBean.setOperator("autotest");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("201", savelivenoticeCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveLiveNotice_005() {
		Logger.start(true,"直播id未输入失败");
		try {
			savelivenoticeBean.setContent("Content");
//			savelivenoticeBean.setLiveId(1);
			savelivenoticeBean.setOperator("autotest");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("400", savelivenoticeCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveLiveNotice_006() {
		Logger.start(true,"直播已关闭修改公告失败");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			new LiveRoomWapper().updateLiveRoomPushStatus(livedto.getId(), 7);
			savelivenoticeBean.setContent("Content");
			savelivenoticeBean.setLiveId(livedto.getId());
			savelivenoticeBean.setOperator("autotest");
			savelivenoticeCall.setData(savelivenoticeBean);
			savelivenoticeCall.callService();			
			Logger.verifyEquals("201", savelivenoticeCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}